﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.model;
using l1cs.server.datatables;
using l1cs.server.templates;
using l1cs.server.serverpackets;

namespace l1cs.server.clientpackets
{
    public class C_SelectList : ClientBasePacket
    {
        private const String _C_SELECT_LIST = "[C] C_SelectList";
        private static Logger _log = Logger.getLogger( typeof( C_SelectList ).FullName );

        public C_SelectList( byte[] abyte0, ClientThread clientthread )
            : base( abyte0 )
        {
            // アイテム毎にリクエストが来る。
            int itemObjectId = readD();
            int npcObjectId = readD();
            L1PcInstance pc = clientthread.getActiveChar();

            if ( npcObjectId != 0 )
            { // 武器の修理
                L1Object obj = L1World.getInstance().findObject( npcObjectId );
                if ( obj != null )
                {
                    if ( obj is L1NpcInstance )
                    {
                        L1NpcInstance npc = (L1NpcInstance)obj;
                        int difflocx = Math.Abs( pc.getX() - npc.getX() );
                        int difflocy = Math.Abs( pc.getY() - npc.getY() );
                        // 3マス以上離れた場合アクション無効
                        if ( difflocx > 3 || difflocy > 3 )
                        {
                            return;
                        }
                    }
                }

                L1PcInventory pcInventory = pc.getInventory();
                L1ItemInstance item = pcInventory.getItem( itemObjectId );
                int cost = item.get_durability() * 200;
                if ( !pc.getInventory().consumeItem( L1ItemId.ADENA, cost ) )
                {
                    return;
                }
                item.set_durability( 0 );
                pcInventory.updateItem( item, L1PcInventory.COL_DURABILITY );
            }
            else
            { // ペットの引き出し
                int petCost = 0;
                int petCount = 0;
                int divisor = 6;
                //Object[] petList = pc.getPetList().values().toArray();
                foreach ( Object pet in pc.getPetList().Values )
                {
                    petCost += ( (L1NpcInstance)pet ).getPetcost();
                }
                int charisma = pc.getCha();
                if ( pc.isCrown() )
                { // 君主
                    charisma += 6;
                }
                else if ( pc.isElf() )
                { // エルフ
                    charisma += 12;
                }
                else if ( pc.isWizard() )
                { // WIZ
                    charisma += 6;
                }
                else if ( pc.isDarkelf() )
                { // DE
                    charisma += 6;
                }
                else if ( pc.isDragonKnight() )
                { // ドラゴンナイト
                    charisma += 6;
                }
                else if ( pc.isIllusionist() )
                { // イリュージョニスト
                    charisma += 6;
                }

                L1Pet l1pet = PetTable.getInstance().getTemplate( itemObjectId );
                if ( l1pet != null )
                {
                    int npcId = l1pet.get_npcid();
                    charisma -= petCost;
                    if ( npcId == 45313 || npcId == 45710 // タイガー、バトルタイガー
                            || npcId == 45711 || npcId == 45712 )
                    { // 紀州犬の子犬、紀州犬
                        divisor = 12;
                    }
                    else
                    {
                        divisor = 6;
                    }
                    petCount = charisma / divisor;
                    if ( petCount <= 0 )
                    {
                        pc.sendPackets( new S_ServerMessage( 489 ) ); // 引き取ろうとするペットが多すぎます。
                        return;
                    }
                    L1Npc npcTemp = NpcTable.getInstance().getTemplate( npcId );
                    L1PetInstance pet = new L1PetInstance( npcTemp, pc, l1pet );
                    pet.setPetcost( divisor );
                }
            }
        }

        override
        public String getType()
        {
            return _C_SELECT_LIST;
        }
    }
}
